<?php
    header('Content-type:text/json');
    require "../connection.php";
    $user_id = $_POST['user_id'];
    $ordermoney = $_POST['ordermoney'];// 获取需要支付的总价

    $return = 0;
    //order 表 是mysql 关键字 不能
    $create_time= date("Y-m-d H:i:s",time());
    //插入订单
    $sql = "insert into orders (user_id,pay_price,create_time,update_time) values('$user_id','$ordermoney','$create_time','$create_time')";
   $res = $conn->doSql($sql);
    //为了插入order_goods 需要订单id
    //得到订单id
    $sql = "select id from orders where user_id = '$user_id'  order by create_time desc limit 1";
    $res = $conn->doSql($sql);
    $order_id = $res[0]['id']; //得到值为1
    //得到商品id，商品数量，商品价格
    $sql = "select goods_id,goods_num,goods_price from cart,goods where goods.id=goods_id and  user_id ='$user_id' and status =1 and ischecked = 1 ";
    $res = $conn->doSql($sql);

    for($i=0,$len=count($res);$i<$len;$i++){
       $sql_insert_ordergoods = "insert into order_goods (order_id,goods_id,goods_num,goods_price) value('$order_id',".$res[$i]['goods_id'].",".$res[$i]['goods_num'].",".$res[$i]['goods_price'].")";
       $result = $conn->doSql($sql_insert_ordergoods);
       if($result){
            $return = $result;
       }
    }
    echo json_encode($return);
 ?>



